<?php

namespace Admin\Controller;

class StorageController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Storage");
    }

    //首页
    public function index() {
        $requestMap = $this->convertRequestToMap();
        unset($requestMap["pageSize"]);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["sr.enterprise_code"] = $user["enterprise_code"];
        if(!isNullOrEmpty($requestMap["code-like"])){
            if($requestMap["sr#type"] == "物料"){
                $requestMap["c#full_name-like"] = $requestMap["code-like"];
            }
            if($requestMap["sr#type"] == "成品矫形器"){
                $requestMap["fp#number-like"] = $requestMap["code-like"];
            }
            unset($requestMap["code-like"]);
        }
        if(!isNullOrEmpty($requestMap["name-like"])){
            if($requestMap["sr#type"] == "物料"){
                $requestMap["m#material_name-like"] = $requestMap["name-like"];
            }
            if($requestMap["sr#type"] == "成品矫形器"){
                $requestMap["fp#name-like"] = $requestMap["name-like"];
            }
            unset($requestMap["name-like"]);
        }
//        if(!isNullOrEmpty($_REQUEST['codeSearch'])){
//            $requestMap["c#name-like"] = $_REQUEST['codeSearch'];
//            $this->assign('codeSearch', $_REQUEST['codeSearch']);
//        }

        //分页
        $count = $this->currentModel->getListCount($requestMap);

        $pageSize = $_REQUEST["pageSize"];
        if(isNullOrEmpty($pageSize)){
            $pageSize = 50;
            $page = $this->page($count, $pageSize);
        }else{
            if($pageSize == -1){
                $page = $this->page($count, 0);
            }else{
                $page = $this->page($count, $pageSize);
            }
        }
        $this->assign('pageSize', $pageSize);

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign('user', $user);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }
    public function remove() {

        $id = $_REQUEST['id'];
        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("Storage/index"));

    }

    public function getTable()
    {
        $ids = $_REQUEST["id"];
        $map['class_name'] = "reporting_storage";
        $formDict = D("FormDict")->queryForList($map);

        $data = array();
        $data["header"] = array();
        $data["volume"] = array();
        $data["list"] = array();
        foreach ($formDict as $key => $value){
            array_push($data["header"], $value);
            array_push($data["volume"], $key);
        }

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        //获取列表
        $list = D("Storage")->alias("sr")
            ->join("left join __ADMIN__ emp on emp.employee_id = sr.update_id")
            ->join("left join __STORE__ s on s.store_id = sr.store_id")
            ->join("left join __SUPPLIER__ sl on sl.supplier_id = sr.supplier_id")
            ->join("left join __CODE__ c on c.code_id = sr.code_id")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->join("left join __MATERIAL__ m on m.code_id = sr.code_id")
            ->join("left join __FINISHED_PRODUCT__ fp on fp.finished_product_id = sr.finished_product_id")
            ->join("left join __CLASS__ cl on cl.class_id = fp.class_id")
            ->join("left join __INSTORE__ isr on isr.instore_id = sr.instore_id")
            ->join("left join __CUSTOMIZED_PRODUCT__ cp on cp.customized_product_id = isr.code_id")
            ->join("left join __WORKORDER__ wo on wo.workorder_id = isr.product_id")
            ->where(array('sr.enterprise_code'=>$user["enterprise_code"]))
            ->order("sr.update_time desc")
            ->field("sr.*, emp.admin_name, s.store_name, sl.supplier_name, c.name as code_name, p.name as parent_name, c.full_name, m.material_name, m.spec, m.prop, m.unit as material_unit, m.barcode as material_barcode, fp.name as finished_product_name, fp.number as finished_product_code, fp.barcode as finished_product_barcode, fp.unit as finished_product_unit, cl.class_name, isr.inventory_name, isr.spec as instore_spec, isr.unit as instore_unit, isr.barcode as instore_barcode, cp.customized_product_code, wo.batch_no as instore_batch_no, wo.price as instore_price")
            ->select();

//        $ids = $_REQUEST["id"];
        $idArr = explode(',', $ids);
        foreach ($list as $item){
            if(in_array($item["storage_id"], $idArr)){
                if($item["type"] == "物料"){
                    $item["storage_code"] = $item["parent_name"].".".$item["code_name"];
                    $item["storage_name"] = $item["material_name"];
                    $item["storage_spec"] = $item["spec"];
                    $item["storage_prop"] = $item["prop"];
                    $item["storage_barcode"] = $item["material_barcode"];
                    $item["storage_unit"] = $item["material_unit"];
                }
                if($item["type"] == "成品矫形器"){
                    $item["storage_code"] = $item["finished_product_code"];
                    $item["storage_name"] = $item["finished_product_name"];
                    $item["storage_spec"] = $item["size"];
                    $item["storage_prop"] = $item["class_name"];
                    $item["storage_barcode"] = $item["finished_product_barcode"];
                    $item["storage_unit"] = $item["finished_product_unit"];
                }
                if($item["type"] == "定制矫形器"){
                    $item["storage_code"] = $item["customized_product_code"];
                    $item["storage_name"] = $item["inventory_name"];
                    $item["storage_spec"] = $item["instore_spec"];
//                    $item["storage_prop"] = ;
                    $item["storage_barcode"] = $item["instore_barcode"];
                    $item["batch_no"] = " ".$item["instore_batch_no"];
                    $item["storage_unit"] = $item["instore_unit"];
                    $item["price"] = $item["instore_price"];
                }
                $item["roundPrice"] = sprintf("%.2f", $item["price"]);
                $totalPrice = $item["price"] * $item["storage_number"];
                $item["roundTotelPrice"] = sprintf("%.2f", $totalPrice);
//                $item["delivery_number"] = " " . $item["delivery_number"];
//                $item["invoice_number"] = " " . $item["invoice_number"];
//                switch ($item["status"]){
//                    case '-2' :
//                        $item["status"] = "已关闭";
//                        break;
//                    case '-1' :
//                        $item["status"] = "待处理";
//                        break;
//                    case '1' :
//                        $item["status"] = "已出库";
//                        break;
//                }
                array_push($data["list"], $item);
            }
        }
//        $fileName = "定制产品报表_".date('Ymd',time());
        $fileName = "实时库存报表_";
        $fileName .= "_" . date('Ymd',time());
        exportExcel($data, $fileName);
    }
}